package main

import (
	"8SSEDT/init_matrix"
	"8SSEDT/load"
	"8SSEDT/save"
	"8SSEDT/sdf"
	"fmt"
	"sync"
)

var wg sync.WaitGroup

func main() {
	//加载png图片
	length, width, img, err := load.LoadPhoto("./sample/S500.png")
	if err != nil {
		fmt.Println(err)
		return
	}
	//获取遮罩和初始化矩阵
	var masks [][]bool
	var matrix [][]*init_matrix.Point
	wg.Add(2)
	//获取遮罩
	go func(masks *[][]bool, err error) {
		defer wg.Done()
		*masks, err = init_matrix.GetPhotoMask(img, length, width, 0.5)
		if err != nil {
			fmt.Println(err)
			*masks = nil
		}
	}(&masks, err)
	//矩阵初始化
	go func(matrix *[][]*init_matrix.Point, err error) {
		defer wg.Done()
		*matrix = init_matrix.InitMatrix(length, width)
	}(&matrix, err)
	wg.Wait()
	if masks == nil {
		return
	}
	//生成sdf
	res := sdf.GenerateSDF(length, width, matrix, masks)
	if res == nil {
		fmt.Println("sdf is nil")
		return
	}
	//保存
	err = save.SaveInPNG(length, width, res, "./demo.png")
	if err != nil {
		fmt.Println(err)
	}
}
